////////////////////////////////////////
//	File Header
//	Author: Stephen Ringer | stephen.ringer@gmail.com
//	File: SAGANetwork.cpp
//
////////////////////////////////////////

////////////////////////////////////////
//	File Includes
#include "SAGANetwork.h"
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: SAGANetwork
//	Purpose:
//		Constructor: Nothing spectacular.
SAGANetwork::SAGANetwork(HINSTANCE hInst) 
{
	m_hInst   = hInst;
	m_pDevice = NULL;
	m_hDLL    = NULL;
}
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: SAGANetwork
//	Purpose:
//		Destructor: Just call the Release method
SAGANetwork::~SAGANetwork() 
{ Release(); }
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: CreateDevice
//	Purpose:
//		Create the dll objects. This functions loads the appropriate dll.
HRESULT SAGANetwork::CreateDevice() 
{
	// load the DLL containing interface implementation
	m_hDLL = LoadLibraryEx("SAGAWS.dll",NULL,0);
	if(!m_hDLL) 
	{
		MessageBox(NULL, "Loading SAGAWS.dll from lib failed.", "SAGAEngine - error", MB_OK | MB_ICONERROR);
		return E_FAIL;
	}

	CREATENETWORKDEVICE _CreateNetworkDevice = 0;
	HRESULT hr;

	// function pointer to dll's 'CreateNetworkDevice' function
	_CreateNetworkDevice = (CREATENETWORKDEVICE)GetProcAddress(m_hDLL, "CreateNetworkDevice");
	// call dll's create function
	hr = _CreateNetworkDevice(m_hDLL, &m_pDevice);
	if(FAILED(hr))
	{
		MessageBox(NULL, "CreateNetworkDevice() from lib failed.", "SAGAEngine - error", MB_OK | MB_ICONERROR);
		m_pDevice = NULL;
		return E_FAIL;
	}

return S_OK;
} 
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: Release
//	Purpose:
//		Cleanup the dll objects.
void SAGANetwork::Release() 
{
	RELEASENETWORKDEVICE _ReleaseNetworkDevice = 0;
	HRESULT hr;

	if (m_hDLL) 
	{
		// function pointer to dll 'ReleaseNetworkDevice' function
		_ReleaseNetworkDevice = (RELEASENETWORKDEVICE)
		 GetProcAddress(m_hDLL, "ReleaseNetworkDevice");
	}
	// call dll's release function
	if (m_pDevice) 
	{
		hr = _ReleaseNetworkDevice(&m_pDevice);
		if(FAILED(hr)) m_pDevice = NULL;
	}
} 
//
////////////////////////////////////////